package com.chance.cc.crawler.development.command.job.domain.xhs.applets;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequest;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.development.scripts.xiaohongshu.applets.XHSAppletsAccountCrawlerScript;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerIP;
import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerPort;

/**
 * @author bx
 * @date 2020/12/6 0006 12:44
 */
public class XHSBDAccountNotesTraceCrawlerScheduleJob {

    public static String domainId = "xhs";
    private static CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(metaServerIP,metaServerPort);


    public static void main(String[] args) {

        publishScheduleJobInfo();
        System.out.println(crawlerSchduler().generateCrawlerTriggerKey());
    }

    public static CrawlerJob publishScheduleJobInfo(){


        CrawlerJob crawlerSchduler =  crawlerSchduler();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerSchduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("小红书账户追溯定时采集");
        crawlerScheduleJob.setCrawlerKey(crawlerSchduler.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布crawler作业：" + page.getRawText());
        return crawlerSchduler;

    }

    /**
     * dy采集
     * @return
     */
    public static CrawlerJob crawlerSchduler() {
        CrawlerRequestRecord crawlerStartRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId,turnPage)
                .httpUrl("") //置空，保证不会进入任何脚本
                .releaseTime(System.currentTimeMillis())
                .build();
        crawlerStartRequestRecord.getHttpConfig().setResponseTextGenerateHtml(false);

        String articleAccumQueueName = "xhs-article-accumulate-queue";
        String requestQueueName = "crawler-xhs-xhs-bd-article-queue";
        int hourFromNow = 24*7;

        CrawlerRequestRecord initAccountHistoryNotesRecord = CrawlerRequestRecord.builder()
                .startPageRequest("xhs_notes_trace",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/crawler/domain/common/api/v1/xhs/account/history/notes/init?" +
                        "articleAccumQueueName="+articleAccumQueueName+"&requestQueueName="+requestQueueName+"&hourFromNow="+hourFromNow)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest("xhs", CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl(XHSAppletsAccountCrawlerScript.startListUrl)
                .releaseTime(System.currentTimeMillis())
                .build();
        requestRecord.tagsCreator().bizTags().addDomain("xhs");
        requestRecord.tagsCreator().bizTags().addSite(XHSAppletsAccountCrawlerScript.site);

        HttpRequest httpRequest = initAccountHistoryNotesRecord.getHttpRequest();
        httpRequest.setMethod("POST");
        httpRequest.setRequestBody(HttpRequestBody.json(JSON.toJSONString(requestRecord),"utf-8"));

        return CrawlerJob.builder()
                .triggerInfo(
                        domainId,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-", domainId,"trace", XHSAppletsAccountCrawlerScript.site, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .consoleResultPipeline(null)
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith(
                        "-","crawler","trace",domainId,XHSAppletsAccountCrawlerScript.site,"queue")))
                .requestRecord(crawlerStartRequestRecord)
                .supportRecord(initAccountHistoryNotesRecord)
                .build();
    }

}
